'''
Created on 2011-9-29

@author: leo
'''
import wx
import urllib2
import re
import sys
from timer import timer
from TaskBarIcon import TaskBarIcon 
class MyFrame(wx.Frame):
    def __init__(self,gurl,guser,gpwd):
        self.thread = None
        wx.Frame.__init__(self, None, -1, "Dr.com For Ubuntu", size=(300,170),style=wx.SYSTEM_MENU|wx.CLOSE_BOX|wx.CAPTION)
        self.SetIcon(wx.Icon(name='mid.ico', type=wx.BITMAP_TYPE_ICO))
        panel = wx.Panel(self, -1)
        wx.StaticText(panel, -1, "server:", pos=(10, 12))
        wx.StaticText(panel,-1,"user:",pos=(10,42))
        wx.StaticText(panel,-1,"password:",pos=(10,72))
        self.statusbar = self.CreateStatusBar()
        self.statusbar.SetFieldsCount(3)
        self.statusbar.SetStatusWidths([-2, -2, -2])
        self.SetStatusText("State")
        self.SetStatusText("By leo108",2)
        self.ip = wx.TextCtrl(panel, -1, gurl, pos=(80, 10),size=(160, 20)) 
        self.user = wx.TextCtrl(panel, -1, guser, pos=(80, 40),size=(160, 20)) 
        self.pwd = wx.TextCtrl(panel, -1, gpwd, pos=(80, 70),size=(160, 20),style=wx.TE_PASSWORD)
        self.login = wx.Button(panel,-1,"login",pos=(20,100))
        self.logout = wx.Button(panel,-1,"logout",pos=(110,100))
        self.hide = wx.Button(panel,-1,"hide me",pos=(200,100))
        self.Bind(wx.EVT_BUTTON, self.Login, self.login)
        self.Bind(wx.EVT_BUTTON, self.Logout, self.logout)
        self.Bind(wx.EVT_BUTTON, self.OnHide, self.hide)
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.Bind(wx.EVT_ICONIZE, self.OnIconfiy)
    def OnHide(self, event):
        self.taskBarIcon = TaskBarIcon(self) 
        self.Hide()
    def OnIconfiy(self, event):
        event.Skip()
    def OnClose(self, event):
        if self.thread != None:
            if self.thread.isAlive():
                self.thread.stop()
        self.Destroy()
    def Login(self,event):
        self.url = self.ip.Value
        self.data = "DDDDD="+self.user.Value+"&upass="+self.pwd.Value+"&0MKKey=%B5%C7%C2%BC+Login"
        res = urllib2.urlopen(self.url, self.data)
        html = res.read() 
        res.close()
        html = html.decode('gbk').encode('utf-8')
        if html.find("successfully")== -1:
            self.SetStatusText("failed!",0)
        else:
            self.SetStatusText("success!",0)
            self.saveconfig(self.url, self.user.Value, self.pwd.Value)
            res = urllib2.urlopen(self.url)
            html = res.read() 
            res.close()
            html = html.decode('gbk').encode('utf-8')
            self.p = re.compile("fee='([^/s]+?)';",re.M)
            self.m = self.p.findall(html)
            money = eval(self.m[0])
            money = 1.0*money/10000
            self.SetStatusText("money:"+str(money),1)
            self.thread = timer(self.url,self.data)
            self.thread.start();
    def Logout(self,event):
        urllib2.urlopen(self.ip.Value+"/F.htm")
        if self.thread:
            self.thread.stop()
        self.SetStatusText("Logouted!",0)
    def saveconfig(self,surl,suser,spwd):
        self.file = open(sys.argv[0],'r')
        try :
            self.text = self.file.read()
            self.file.close()
            self.p = re.compile(r'^gurl="(.*)"',re.M)
            self.text = self.p.sub(r'gurl="'+surl+'"',self.text)
            self.p = re.compile(r'^guser="(.*)"',re.M)
            self.text = self.p.sub(r'guser="'+suser+'"',self.text)
            self.p = re.compile(r'^gpwd="(.*)"',re.M)
            self.text = self.p.sub(r'gpwd="'+spwd+'"',self.text)
        finally :
            self.file.close() 
        if self.text != '':
            try:
                self.file = open(sys.argv[0],'w')
                self.file.write(self.text)
            finally:
                self.file.close()